layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'table', 'jquery', 'winui', 'form'], function (exports) {
    winui.renderColor();
    var $ = layui.$,
        form = layui.form,
        table = layui.table;

    // 获取车间数据
    function fetchWorkShop() {
        AjaxPostUtil.request({
            url: sysMainMation.erpBasePath + 'queryStaffBelongFarmList',
            params: {},
            type: 'json',
            method: "GET",
            callback: function (json) {
                var options = '<option value="">请选择车间</option>';
                json.rows.forEach(function (workshop) {
                    options += '<option value="' + workshop.id + '">' + workshop.name + '</option>';
                });
                $('select[name="workshopId"]').html(options);
                form.render('select');
                // 自动选中第一个车间
                if (json.rows.length > 0) {
                    $('select[name="workshopId"]').val(json.rows[0].id);
                    form.render('select');
                    loadTable();
                }
            }
        });
    }

    // 获取表格参数
    function getTableParams() {
        var params = $.extend(true, '', initTableSearchUtil.getSearchValue("messageTable"));
        params.holderId = $('select[name="workshopId"]').val();
        return params;
    }

    // 初始化表格
    function initTable() {
        table.render({
            id: 'messageTable',
            elem: '#messageTable',
            method: 'post',
            url: sysMainMation.checkworkBasePath + 'querySchedulingList',
            where: getTableParams(),
            even: false,
            page: true,
            limits: getLimits(),
            limit: getLimit(),
            cols: [[
                { title: systemLanguage["com.skyeye.serialNumber"][languageType], type: 'numbers' },
                { title: '班次名称', templet: function (d) { return d.shiftMation ? d.shiftMation.name : ''; }, align: 'left', width: 200 },
                { title: '最大需求人数', templet: function (d) { return d.shiftMation ? d.shiftMation.maxStaff : ''; }, align: 'left', width: 120 },
                { title: '最小需求人数', templet: function (d) { return d.shiftMation ? d.shiftMation.minStaff : ''; }, align: 'left', width: 150 },
                { field: 'createName', title: systemLanguage["com.skyeye.createName"][languageType], width: 140 },
                { field: 'createTime', title: systemLanguage["com.skyeye.createTime"][languageType], align: 'center', width: 150 },
                { field: 'lastUpdateName', title: systemLanguage["com.skyeye.lastUpdateName"][languageType], align: 'left', width: 140 },
                { field: 'lastUpdateTime', title: systemLanguage["com.skyeye.lastUpdateTime"][languageType], align: 'center', width: 150 },
                { title: systemLanguage["com.skyeye.operation"][languageType], fixed: 'right', align: 'center', width: 100, toolbar: '#tableBar' }
            ]],
            done: function (json) {
                matchingLanguage();
            }
        });

        table.on('tool(messageTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'delet') { // 删除
                delet(data);
            } else if (layEvent === 'edit') { // 编辑
                edit(data);
            }
        });
    }

    // 添加
    $("body").on("click", "#addBean", function () {
        var url = "../../tpl/rosterManage/writeRosterManage.html?workshopId=" + $('select[name="workshopId"]').val();
        window.open(url, '_blank');
    });

    // 编辑
    function edit(data) {
        var url = "../../tpl/rosterManage/writeRosterManage.html?id=" + data.id + "&workshopId=" + $('select[name="workshopId"]').val() + "&isEdit=true";
        window.open(url, '_blank');
    }

    // 删除
    function delet(data) {
        layer.confirm(systemLanguage["com.skyeye.deleteOperationMsg"][languageType], { icon: 3, title: systemLanguage["com.skyeye.deleteOperation"][languageType] }, function (index) {
            parent.layer.close(index);
            AjaxPostUtil.request({
                url: sysMainMation.checkworkBasePath + "deleteSchedulingByIds",
                params: { ids: data.id }, type: 'json', method: "DELETE",
                callback: function (json) {
                    winui.window.msg(systemLanguage["com.skyeye.deleteOperationSuccessMsg"][languageType], { icon: 1, time: 2000 });
                    loadTable();
                }
            });
        });
    }

    // 车间选择变化
    form.on('select(workshopId)', function (data) {
        loadTable();
    });

    // 刷新表格
    $("body").on("click", "#reloadTable", function () {
        loadTable();
    });

    function loadTable() {
        table.reloadData("messageTable", { where: getTableParams() });
    }

    // 初始化
    initTable();
    fetchWorkShop();
    form.render();

    exports('rosterManageList', {});
});
